package Mystack;

import java.util.Stack;
import java.lang.String;

public class Test {

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == '(' || ch == '[' || ch == '{') {
                stack.push(ch);
            } else {
                if (stack.empty()) {
                    return false;
                }
                char top = stack.peek();
                if ((ch == '(' && top == ')') ||
                        (ch == '{' && top == '}') ||
                        (ch == '[' && top == ']')) {
                    stack.pop();
                } else {
                    return false;
                }
            }
        }
        if (!stack.empty()) {
            return false;
        }
        return true;

    }

    public static void main(String[] args) {
        Mystack mystack = new Mystack();
        mystack.push(1);
        mystack.push(2);
        mystack.push(3);
        mystack.push(4);
        mystack.show();

        mystack.pop();
        mystack.show();
        //mystack.size();
    }

    public boolean isValid1(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == '(' || ch == '{' || ch == '[') {
                stack.push(ch);
            } else {
                char pop = stack.peek();
                if (ch == ')' && pop == '(' ||
                        ch == '}' && pop == '{' ||
                        ch == ']' && ch == '[') {
                    stack.pop();
                } else {
                    return false;
                }
            }
        }
        if (!stack.empty()) {
            return false;
        }
        return true;
    }


    public int evalRPN1(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            String tmp = tokens[i];
            if(!isOperations(tmp)){
                stack.push(Integer.parseInt(tmp));
            }else{
                Integer val1 = stack.pop();
                Integer val2 = stack.pop();
                switch (tmp) {
                    case "+":
                        stack.push(val1 + val2);
                        break;
                    case "-":
                        stack.push(val1 - val2);
                        break;
                    case "*":
                        stack.push(val1 * val2);
                        break;
                    case "/":
                        stack.push(val1 / val2);
                        break;
                }
            }
        }
        return stack.pop();
    }



    public boolean isOperations1(String tmp) {
        if (tmp.equals("*") ||
                tmp.equals("-") ||
                tmp.equals("+") ||
                tmp.equals("*")) {
            return true;
        } else {
            return false;
        }
    }

    public int evalRPN(String[] tokens){
        Stack<Integer> stack=new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            String tmp = tokens[i];
            if (!isOperations(tmp)){
                stack.push(Integer.parseInt(tmp));
            }else {
                int val1 = stack.pop();
                int val2 = stack.pop();
                switch (tmp){
                    case "+": stack.push(val2+val1);
                        break;
                    case "-": stack.push(val2-val1);
                        break;
                    case "*": stack.push(val2*val1);
                        break;
                    case "/": stack.push(val2/val1);
                        break;
                }
            }
        }
        return stack.pop();
    }

    public boolean isOperations(String tmp){
        if (tmp.equals("+")||tmp.equals("-")||
                tmp.equals("*")||tmp.equals("/")){
            return true;
        }
        return false;
    }


}
